/*
 * @Author: your name
 * @Date: 2021-10-12 10:57:10
 * @LastEditTime: 2021-10-13 15:25:24
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: /a1902-a-group/admin_pc/utils/http.js
 */

import axios from 'axios'
import Cookie from 'js-cookie'
// 添加请求拦截器

axios.defaults.baseURL = '/api'; // 统一接口请求 http://127.0.0.1:7002/ === /api

axios.defaults.timeout = 6000; // 请求时常

axios.interceptors.request.use(function (config) {
    // 在发送请求之前做些什么return config;
    const token = Cookie.get('token');
    if(token) {
        // config.headers['authorization']
        config.headers.authorization = token;
    }
    return config;
  }, function (error) {
    // 对请求错误做些什么
    return Promise.reject(error);
});

// 添加响应拦截器
axios.interceptors.response.use(function (response) {
    // 对响应数据做点什么
    return response;
  }, function (error) {
    console.log(error)
    // 对响应错误做点什么
    return Promise.reject(error);
});


/**
 * @description: get请求
 * @param {url 请求地址}
 * @param {params 参数}
 * @return {接口数据}
 */
export const getHttp = async (url, params) => {
    return await axios.get(url, {params})
}


/**
 * @description: post请求
 * @param {url 请求地址}
 * @param {params 参数}
 * @return {接口数据}
 */
export const postHttp = async (url, params) => {
    return await axios.post(url, params);
}
